package com.yunfei.api.message;
import com.yunfei.api.message.contants.AppMessageTemplateCenterContants;
import com.yunfei.api.message.param.template.FindAllDataList.MessageTemplateFindAllDataListParam;
import com.yunfei.api.message.param.template.edite.MessageTemplateEditeParam;
import com.yunfei.api.message.responseBody.templage.findAllDataList.MessageTemplateFindAllDataList;
import com.yunfei.api.message.responseBody.templage.findAllDataList.MsgRuleConfigSimpleBo;
import com.yunfei.api.message.responseBody.templage.initPageData.MessageTemplateInitPageData;
import com.yunfei.bo.result.ResultModel;
import com.yunfei.bo.tDictionary.TdictionarySimpleBo;
import com.yunfei.entity.msgRuleConfig.MsgRuleConfigEntity;
import com.yunfei.enums.ResultStatus;
import com.yunfei.fegin.RedisFeignService;
import com.yunfei.mapper.aiiidb.MsgRuleConfigMapper;
import com.yunfei.mapper.aiiidb.TPersonalUserMapper;
import com.yunfei.mapper.aiiidb.TdictionaryMapper;
import com.yunfei.ribbon.RedisServiceRibbon;
import com.yunfei.service.RedisService;
import com.yunfei.utils.string.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Description: 消息中心
 * @Author: zhengyunfei
 * @Date: 2018/6/15
 * @time:11:26
 */
@SuppressWarnings("ALL")
@Api(value = AppMessageTemplateCenterContants.MESSAGE_TEMPLATE_CENTER_API_NAMESPACE,tags = AppMessageTemplateCenterContants.MESSAGE_TEMPLATE_CENTER_API_NAMESPACE )
@RestController
@RequestMapping("/api/message")
public class AppMessageTemplateCenterApiController {
    @Autowired
    private TPersonalUserMapper tPersonalUserMapper;
    @Autowired
    private RedisFeignService redisFeignService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private RedisServiceRibbon redisServiceRibbon;
    @Autowired
    private MsgRuleConfigMapper msgRuleConfigMapper;
    @Autowired
    private TdictionaryMapper tdictionaryMapper;
    @Autowired
    private Environment env;
    @GetMapping(value = "/templateManager/initPageData",produces = "application/json;charset=UTF-8")
    @ApiOperation(value = AppMessageTemplateCenterContants.MESSAGE_MANAGER_INITPAGE_API,notes = AppMessageTemplateCenterContants.AUTHOR_NOTE,position = 1,response = MessageTemplateInitPageData.class)
    public Object initPage(){
        ResultModel resultModel=new ResultModel();
        try{
            Map<String,Object> resultMap=new HashMap<>();
            //发送时间类型
            List<TdictionarySimpleBo> sendTimeType=tdictionaryMapper.getSendTimeType();
            //消息对象类型
            List<TdictionarySimpleBo> msgObject=tdictionaryMapper.getMsgObject();
            resultMap.put("sendTimeType",sendTimeType);
            redisService.set("sendTimeType",sendTimeType);
            resultMap.put("msgObject",msgObject);
            redisService.set("msgObject",msgObject);
            resultModel.setData(resultMap);
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
        }catch (Exception e){
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
        }
        return resultModel;
    }

    /**
     * @Description: 消息模板管理，查询列表
     * @param msgRuleName 消息规则名称
     * @param sendTimeType 发送时间类型
     * @param msgObject 消息对象类型
     * @Author: zhengyunfei
     * @Date: 2018/10/30
     * @time:9:13
     */
    @PostMapping(value = "/templateManager/findAllDataList",produces = "application/json;charset=UTF-8")
    @ApiOperation(value = AppMessageTemplateCenterContants.MESSAGE_MANAGER_API,notes = AppMessageTemplateCenterContants.AUTHOR_NOTE,position = 1,response = MessageTemplateFindAllDataList.class)
    public Object findAllDataList(MessageTemplateFindAllDataListParam param){
        ResultModel resultModel=new ResultModel();
        try{
            String msgRuleName=param.getMsgRuleName();
            String sendTimeType=param.getSendTimeType();
            String msgObject=param.getMsgObject();
            String pageNow=param.getPageNow();
            Map<String,Object> queryMap=new HashMap<>();
            queryMap.put("msgRuleName",msgRuleName);
            queryMap.put("sendTimeType",sendTimeType);
            queryMap.put("msgObject",msgObject);
            int page=1;
            int pageSize=20;
            if(!StringUtil.isNullOrEmpty(pageNow)){
                page=Integer.parseInt(pageNow);
            }
            queryMap.put("curNo",(page-1)*pageSize);
            queryMap.put("curSize",page*pageSize);
            List<MsgRuleConfigEntity> resultList= msgRuleConfigMapper.findAllListPage(queryMap);
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
            resultModel.setData(resultList);
        }catch (Exception e){
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
        }
        return resultModel;
    }
    /**
     * @Description: 消息模板管理，编辑获取数据
     * @param msgRuleName 消息规则名称
     * @param sendTimeType 发送时间类型
     * @param msgObject 消息对象类型
     * @Author: zhengyunfei
     * @Date: 2018/10/30
     * @time:9:13
     */
    @PostMapping(value = "/templateManager/edite",produces = "application/json;charset=UTF-8")
    @ApiOperation(value = AppMessageTemplateCenterContants.MESSAGE_MANAGER_EDITE_API,notes = AppMessageTemplateCenterContants.AUTHOR_NOTE,position = 1,response = MessageTemplateFindAllDataList.class)
    public Object edite(MessageTemplateEditeParam param){
        ResultModel resultModel=new ResultModel();
        try{
            String msgCfgId=param.getMsgCfgId();
            Map<String,Object> queryMap=new HashMap<>();
            queryMap.put("msgCfgId",msgCfgId);
            MsgRuleConfigSimpleBo entity= (MsgRuleConfigSimpleBo) msgRuleConfigMapper.get(queryMap);
            //发送时间类型转中文
            String sendTimeTypeKey=entity.getSendTimeType();
            //消息对象类型转中文
            String msgObjectKey=entity.getMsgObject();
            List<TdictionarySimpleBo> sendTimeTypeList= (List<TdictionarySimpleBo>) redisService.get("sendTimeType");
            List<TdictionarySimpleBo> msgObjectList= (List<TdictionarySimpleBo>)  redisService.get("msgObject");
            String msgObjectKeyValue=getKeyValue(msgObjectList,msgObjectKey);
            String sendTimeTypeKeyValue=getKeyValue(sendTimeTypeList,sendTimeTypeKey);
            entity.setSendTimeType(sendTimeTypeKeyValue);
            entity.setMsgObject(msgObjectKeyValue);
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
            resultModel.setData(entity);
        }catch (Exception e){
            e.printStackTrace();
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
        }
        return resultModel;
    }
    /**
     * @Description: 消息模板管理，编辑获取数据
     * @param msgRuleName 消息规则名称
     * @param sendTimeType 发送时间类型
     * @param msgObject 消息对象类型
     * @Author: zhengyunfei
     * @Date: 2018/10/30
     * @time:9:13
     */
    @PostMapping(value = "/templateManager/update",produces = "application/json;charset=UTF-8")
    @ApiOperation(value = AppMessageTemplateCenterContants.MESSAGE_MANAGER_UPDATE_API,notes = AppMessageTemplateCenterContants.AUTHOR_NOTE,position = 1,response = MessageTemplateFindAllDataList.class)
    public Object update(MsgRuleConfigSimpleBo param){
        ResultModel resultModel=new ResultModel();
        try{
            //发送时间类型转中文
            String sendTimeType=param.getSendTimeType();
            //消息对象类型转中文
            String msgObject=param.getMsgObject();
            List<TdictionarySimpleBo> sendTimeTypeList= (List<TdictionarySimpleBo>) redisService.get("sendTimeType");
            List<TdictionarySimpleBo> msgObjectList= (List<TdictionarySimpleBo>)  redisService.get("msgObject");
            String msgObjectKey=getKey(msgObjectList,msgObject);
            String sendTimeTypeKey=getKey(sendTimeTypeList,sendTimeType);
            param.setSendTimeType(sendTimeTypeKey);
            param.setMsgObject(msgObjectKey);
            msgRuleConfigMapper.update(param);
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
            resultModel.setData("更新成功");
        }catch (Exception e){
            e.printStackTrace();
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
        }
        return resultModel;
    }
    /**
    * @Description: 将key转换为value
    */
public static String getKeyValue( List<TdictionarySimpleBo> list,String myKey){
        String value="";
        for(int i=0;i<list.size();i++){
            String keyValue=list.get(i).getcCaption();
            String key=list.get(i).getcKeyvalue();
            if(myKey.equals(key)){
                value=keyValue;
            }
        }
        return value;
    }
    /**
     * @Description: 将key转换为value
     */
    public static String getKey( List<TdictionarySimpleBo> list,String myValue){
        String result="";
        for(int i=0;i<list.size();i++){
            String keyValue=list.get(i).getcCaption();
            String key=list.get(i).getcKeyvalue();
            if(myValue.equals(keyValue)){
                result=key;
            }
        }
        return result;
    }
}
